package com.liu.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/*可以下载网上的资源，只要有url*/
public class GetInternetResource {
    public static void main(String[] args) throws Exception{
//        在这里输入url
        URL url=new URL("https://m801.music.126.net/20210607120605/f96ad46975ade7eb8854073a34a20b21/jdyyaac/005c/565b/055a/76f6070c01a176660aec20d20d069c7c.m4a");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream fis = connection.getInputStream();
        url.getFile();
//        这里设置保存地址
        File file=new File("E:\\A Downloads\\demo02.mp3");
        if (!file.exists()){
            OutputStream os=new FileOutputStream(file);
            byte[]bytes=new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes);
            }
            System.out.println("下载完成！");
        }else {
            System.out.println("该文件已存在！");
        }
    }
}
